package com.buer.storage.service.impl;

import com.buer.storage.dao.StockDao;
import com.buer.storage.dto.SelectStockDto;
import com.buer.storage.entity.Area;
import com.buer.storage.entity.Dorder;
import com.buer.storage.entity.Stock;
import com.buer.storage.entity.Worder;
import com.buer.storage.service.StockService;
import com.buer.storage.utils.DateUtils;
import com.buer.storage.vo.StockVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class StockServiceImpl implements StockService {
	@Resource
	private StockDao stockDao;
	
	@Override
	public Stock get(Integer stockId){
		return stockDao.get(stockId);
	}
	
	@Override
	public List<Stock> list(Map<String, Object> map){
		return stockDao.list(map);
	}

	@Override
	public List<StockVo> getAllStock() {
		return stockDao.getAllStock();
	}

	@Override
	public int count(Map<String, Object> map){
		return stockDao.count(map);
	}
	
	@Override
	public int save(Stock stock){
		return stockDao.save(stock);
	}
	
	@Override
	public int update(Stock stock){
		return stockDao.update(stock);
	}
	
	@Override
	public int remove(Integer stockId){
		return stockDao.remove(stockId);
	}
	
	@Override
	public int batchRemove(Integer[] stockIds){
		return stockDao.batchRemove(stockIds);
	}

    @Override
    public PageInfo<Stock> getStockList(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Stock> list = stockDao.getStockList(map); // 获取数据
        PageInfo<Stock> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public Stock getOne(Map<String, Object> map){
        return stockDao.getOne(map);
    }

	@Override
	public List<SelectStockDto> selectStockNumSum() {
		List<SelectStockDto> selectStockDtos = stockDao.selectStockNumSum();
		return selectStockDtos;
	}


	@Override
	public List<Map> selectNumByDate(){
		List<Map> list=new ArrayList<>();
		for (int i=0;i<7;i++){
			Map map=new HashMap();
			//时间
			String day = DateUtils.formatDate(DateUtils.intervalBeforeDay(new Date(), i), "yyyy-MM-dd");
			//入库数
			int worderNum = stockDao.selectWorderProdNum(day);
			//出库数
			int dorderNum = stockDao.selectDorderProdNum(day);

			map.put("日期",day);
			map.put("今日入库",worderNum);
			map.put("今日出库",dorderNum);

			list.add(map);
		}
		return list;
	}


	@Override
	public List<Map> selectNumByName() {
		List<Map> list=new ArrayList<>();
		List<Area> areaList = stockDao.selectAreaName();
		for (Area area : areaList) {
			Map map=new HashMap();
			//时间
			String day = DateUtils.getDate();
			//出库数
			int worderNum = stockDao.selectWorderNumByName(area.getAreaId(),day);

			int dorderNum = stockDao.selectDorderNumByName(area.getAreaId(),day);

			map.put("库区",area.getAreaName());
			map.put("今日入库",worderNum);
			map.put("今日出库",dorderNum);

			list.add(map);
		}
		return list;
	}

}
